Skip to content

Conversation

thejpster
Copy link
Contributor

Adds a data type for managing a PSP stack, and a function to enter PSP mode using it

Avoids future unsafe-in-unsafe warning,.
The function pointer is branched to by assembly, so we're relying on a
certain ABI.
@thejpster
Copy link
Contributor Author

https://github.com/thejpster/psp-example/blob/main/src/main.rs contains an example showing how to use it.

Now offers both Priv and Unpriv modes, and has a handle to represent
ownership of a static Stack object.

The load/store check on `Stack::taken` is not perfectly thread safe,
but it's probably good enough and doing better requires a
critical-section or CAS atomics.
@thejpster
Copy link
Contributor Author

Updated the demo to match

@thejpster
Copy link
Contributor Author

The asm is not Armv6-M compatible but inline(always) hides the issue and allows the code to build.

See https://rust.godbolt.org/z/sYWMGah8b and #t-compiler > inline(always) caused inline assembly to not get checked

@thejpster
Copy link
Contributor Author

Until I added another function that called the broken function anyway, then it suddently noticed the assembly was wrong

@thejpster
Copy link
Contributor Author

Tested with https://github.com/thejpster/psp-example

$ cargo run --bin unpriv_demo                              
   Compiling cortex-m v0.7.7 (https://github.com/thejpster/cortex-m?branch=add-unprivileged-mode#ef8164b7)
   Compiling cortex-m-rt v0.7.5 (https://github.com/thejpster/cortex-m?branch=add-unprivileged-mode#ef8164b7)
   Compiling cortex-m-rt-macros v0.7.5 (https://github.com/thejpster/cortex-m?branch=add-unprivileged-mode#ef8164b7)
   Compiling defmt-semihosting v0.3.0
   Compiling psp-example v0.1.0 (/home/jonathan/Documents/github/thejpster/psp-example)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45s
     Running `/home/jonathan/Documents/github/thejpster/psp-example/./qemu-run.sh target/thumbv7em-none-eabihf/debug/unpriv_demo`
ELF_BINARY=target/thumbv7em-none-eabihf/debug/unpriv_demo
Running on '-cpu cortex-m4 -machine mps2-an386'...
------------------------------------------------------------------------
[INFO ] Using MSP. addr(x) = 203fffc4 (bin/unpriv_demo.rs:17)
[INFO ] PSP stack is at 20000000..20004000 (bin/unpriv_demo.rs:23)
[INFO ] Got SVCall, ptr=20003fec (bin/unpriv_demo.rs:62)
[INFO ] Got SVCall, ptr=20003ff0 (bin/unpriv_demo.rs:62)
[INFO ] Got SVCall, ptr=00000000 (bin/unpriv_demo.rs:62)
[INFO ] Got exit request, leaving now! (bin/unpriv_demo.rs:64)
------------------------------------------------------------------------
$ cargo run --bin priv_demo  
   Compiling psp-example v0.1.0 (/home/jonathan/Documents/github/thejpster/psp-example)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.07s
     Running `/home/jonathan/Documents/github/thejpster/psp-example/./qemu-run.sh target/thumbv7em-none-eabihf/debug/priv_demo`
ELF_BINARY=target/thumbv7em-none-eabihf/debug/priv_demo
Running on '-cpu cortex-m4 -machine mps2-an386'...
------------------------------------------------------------------------
[INFO ] Hello! (bin/priv_demo.rs:14)
[INFO ] Using MSP. addr(x) = 203fffc0 (bin/priv_demo.rs:15)
[INFO ] PSP stack is at 20000000..20004000 (bin/priv_demo.rs:20)
[INFO ] User mode, ptr=203fff74 (bin/priv_demo.rs:31)
------------------------------------------------------------------------

Turns out we don't check the assembly inside inline(asm) functions until
the function is actually called (or referenced).
@thejpster thejpster force-pushed the add-unprivileged-mode branch from ef8164b to a951c1f Compare October 3, 2025 16:11
@thejpster
Copy link
Contributor Author

Fixed a bug, but had to make some functions const to do it. Also now sets the PSPLIM register.

Also fixes the enter_privilged_function, which forgot to the change the
mode so we didn't actually start using the PSP.
@thejpster thejpster force-pushed the add-unprivileged-mode branch from e3465c1 to ecf5ddc Compare October 3, 2025 17:15
@adamgreig adamgreig enabled auto-merge October 6, 2025 00:57
@adamgreig adamgreig added this pull request to the merge queue Oct 6, 2025
Merged via the queue into rust-embedded:master with commit 1436479 Oct 6, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants